package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.dao.db1.BisBigDao;
import com.sailfish.springbootdemo.dao.db1.BisDao;
import com.sailfish.springbootdemo.dao.db1.BisLineDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.Bis;
import com.sailfish.springbootdemo.pojo.db1.BisBig;
import com.sailfish.springbootdemo.pojo.db1.BisLine;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class BisLineServiceImpl implements BisLineService {

    @Autowired
    private BisLineDao bisLineDao;

    @Autowired
    private BisDao bisDao;

    @Autowired
    private BisBigDao bisBigDao;

    @Qualifier("MysqlTransactionManager1")
    @Autowired
    private DataSourceTransactionManager transactionManager1;

    @Override
    public Result getAllBisByLine(Integer lineId) {
        try {
            if (lineId == null || "".equals(lineId.toString())) {
                return ResultUtil.error(500, "缺少参数lineId", "Miss Param lineId", null);
            }
            List<BisBig> allBisBig = bisBigDao.getAllBisBig();
            for (BisBig bisBig : allBisBig) {
                List<Bis> allBisByBigId = bisDao.getAllBisByBigId(bisBig.getBisBigId());
                for (Bis bis : allBisByBigId) {
                    BisLine bisLine = bisLineDao.getBisLineByLineAndKey(lineId, bis.getBisKey());
                    if (bisLine != null) {
                        bis.setReachStandard(bisLine.getReachStandard());
                        bis.setSysPassYield(bisLine.getSysPassYield());
                        bis.setIsOk(bisLine.getIsOk() != null ? bisLine.getIsOk() : 2);
                        /*if(bisLine.getReachStandard() != null && !"".equals(bisLine.getReachStandard()) && CommonUtils.isNumeric(bisLine.getReachStandard()) &&
                            bisLine.getSysPassYield() != null && !"".equals(bisLine.getSysPassYield()) && CommonUtils.isNumeric(bisLine.getSysPassYield())
                        ){
                            if(Double.parseDouble(bisLine.getReachStandard()) >= Double.parseDouble(bisLine.getSysPassYield())){
                                bis.setIsOk(1);
                            }else{
                                bis.setIsOk(2);
                            }
                        }*/
                    }
                }
                bisBig.setBisList(allBisByBigId);
            }
            return ResultUtil.success(allBisBig);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Transactional(transactionManager = "MysqlTransactionManager1")
    @Override
    public Result submitBisData(String data) {
        try {
            JSONObject jo = JSONArray.parseObject(data);
            String lineId = jo.get("lineId").toString();
            if ("".equals(lineId)) {
                return ResultUtil.error(500, "缺少参数lineId", "Miss Param lineId", null);
            }
            JSONArray ja = jo.getJSONArray("list");
            // 删除原先的
            bisLineDao.deleteBisLineByLine(Integer.parseInt(lineId));
            for (int a = 0; a < ja.size(); a++) {
                JSONObject jsonObject = ja.getJSONObject(a);
                JSONArray bisList = jsonObject.getJSONArray("bisList");
                for (int b = 0; b < bisList.size(); b++) {
                    JSONObject bisObj = bisList.getJSONObject(b);
                    String reachStandard = bisObj.get("reachStandard").toString();
                    String sysPassYield = bisObj.get("sysPassYield").toString();
                    String bisKey = bisObj.get("bisKey").toString();
                    String sort = bisObj.get("sort").toString();
                    Integer isOk = !"".equals(bisObj.get("isOk").toString()) ? bisObj.getInteger("isOk") : 2;


                    BisLine bisLine = new BisLine();
                    bisLine.setBisKey(bisKey);
                    bisLine.setReachStandard(reachStandard);
                    bisLine.setSysPassYield(sysPassYield);
                    bisLine.setSort(Integer.parseInt(sort));
                    bisLine.setLineId(Integer.parseInt(lineId));
                    bisLine.setIsOk(isOk);
                    bisLineDao.addBisLine(bisLine);
                }
            }
            return ResultUtil.success(null);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
